package su.samcom.calc.model.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import su.samcom.calc.model.entity.Route;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Владислав
 * Date: 23.11.13
 * Time: 20:54
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class RouteDaoImpl extends BaseDaoImpl<Route> implements RouteDao {
    @Override
    protected Class<Route> getGeneric() {
        return Route.class;
    }

    @Override
    public Route getRoute(Long locationAId, Long locationBId) {
        String sql = "from Route r where " +
                "(r.locationA.id = :locationAId and r.locationB.id = :locationBId) or " +
                "(r.locationA.id = :locationBId and r.locationB.id = :locationAId)";
        Query query = sessionFactory.getCurrentSession().createQuery(sql);
        query.setParameter("locationAId", locationAId).setParameter("locationBId", locationBId);
        List<Route> routes = query.list();
        if(routes.size() > 0){
            return routes.get(0);
        }
        return null;
    }
}
